/**
 * 后台数据请求接口列表
 */
import { getSysBase } from '@/utils/config'
import type { ApiDataResult, ApiLogoutResult, ApiPageResult, ApiResult } from './interface'
import type { Sys } from './sys/namespaces'
import http from '@/utils/request'
// import type { Login, Menu, Sys } from './namespaces'
/**
 * 添加目录
 * @param params
 * @returns {}
 */
export const saveDirApi = (params: any): Promise<ApiResult> => {
    return http.post<any>('/doc/mgr/saveDir.json', params, {
        loading: true,
        baseURL: getSysBase()
    })
}
export const getloadDirList = (params: any): Promise<ApiResult> => {
  return http.get<any>('/doc/mgr/loadDirList.json', params, {
    loading: true,
    baseURL: getSysBase()
  })
}
export const delDir = (params: any): Promise<ApiResult> => {
  return http.postForm<any>('/doc/mgr/delDir.json', params, {
    loading: true,
    baseURL: getSysBase()
  })
}


/**
 * 上传系统附件信息数据
 * @param {String} typeCode
 * @param {File} file
 * @param {String} dataId
 * @returns {Promise<ApiDataResult<Sys.AttachmentInfo>>}
 */
export const uploadDocApi = (
  file: File,
  entity: any
): Promise<ApiDataResult<Sys.AttachmentInfo>> => {
  const formData = new FormData()
  formData.append('file', file)
  if (entity) {
    const keys = Object.keys(entity)
    for (const key of keys) {
      const value = entity[key]
      if (value) {
        formData.append(key, value)
      }
    }
  }
  return http.post<ApiDataResult<Sys.AttachmentInfo>>('doc/mgr/uploadFile.json', formData, {
    loading: true,
    baseURL: getSysBase(),
    headers: { 'Content-Type': 'multipart/form-data' }
  })
}

// 更具目录id获取文件列表

export const getloadFileList = (params: any): Promise<ApiResult> => {
  return http.get<any>('/doc/mgr/loadFileList.json', params, {
    loading: true,
    baseURL: getSysBase()
  })
}

export const delFile = (params: any): Promise<ApiResult> => {
  return http.postForm<any>('doc/mgr/delFile.json', params, {
    loading: true,
    baseURL: getSysBase()
  })
}

// /**
//  * 下载附件
//  * @param {String} id
//  */
// export const downloadAttachmentInfoApi = (ulr: string) => {
//   location.href = getSysBase() + 'doc/mgr/download?id=' + id
// }

/**
 * 下载附件
 * @param {String} id
 */
export const downloadimg = (url: string) => {
  // getSysBase() +
  location.href =  url
}